package com.waterfall.huewu.pla.sample;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.android.bitmapfun.util.ImageCache;
import com.example.android.bitmapfun.util.ImageFetcher;
import com.waterfall.config.Helper;
import com.waterfall.data.model.DuitangInfo;
import com.waterfall.me.maxwin.view.XListView;
import com.waterfall.me.maxwin.view.XListView.IXListViewListener;
import com.waterfall.widget.ScaleImageView;

public class PullToRefreshSampleActivity extends FragmentActivity implements
	IXListViewListener {
    private ImageFetcher mImageFetcher;
    private XListView mAdapterView = null;
    private StaggeredAdapter mAdapter = null;
    private int currentPage = 0;
    ContentTask task = new ContentTask(this, 2);

    private class ContentTask extends
	    AsyncTask<String, Integer, List<DuitangInfo>> {

	private Context mContext;
	private int mType = 1;

	public ContentTask(Context context, int type) {
	    super();
	    mContext = context;
	    mType = type;
	}

	@Override
	protected List<DuitangInfo> doInBackground(String... params) {
	    try {
		return parseNewsJSON(params[0]);
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	    return null;
	}

	@Override
	protected void onPostExecute(List<DuitangInfo> result) {
	    if (mType == 1) {

		mAdapter.addItemTop(result);
		mAdapter.notifyDataSetChanged();
		mAdapterView.stopRefresh();

	    } else if (mType == 2) {
		mAdapterView.stopLoadMore();
		mAdapter.addItemLast(result);
		mAdapter.notifyDataSetChanged();
	    }

	}

	@Override
	protected void onPreExecute() {
	}

	public List<DuitangInfo> parseNewsJSON(String url) throws IOException {
	    List<DuitangInfo> duitangs = new ArrayList<DuitangInfo>();
	    String json = "";
	    if (Helper.checkConnection(mContext)) {
		try {
		    json = Helper.getStringFromUrl(url);

		} catch (IOException e) {
		    Log.e("IOException is : ", e.toString());
		    e.printStackTrace();
		    return duitangs;
		}
	    }
	    Log.d("MainActiivty", "json:" + json);

	    try {
		if (null != json) {
		    JSONObject newsObject = new JSONObject(json);
		    JSONObject jsonObject = newsObject.getJSONObject("data");
		    JSONArray blogsJson = jsonObject.getJSONArray("blogs");

		    for (int i = 0; i < blogsJson.length(); i++) {
			JSONObject newsInfoLeftObject = blogsJson
				.getJSONObject(i);
			DuitangInfo newsInfo1 = new DuitangInfo();
			newsInfo1
				.setAlbid(newsInfoLeftObject.isNull("albid") ? ""
					: newsInfoLeftObject.getString("albid"));
			newsInfo1
				.setIsrc(newsInfoLeftObject.isNull("isrc") ? ""
					: newsInfoLeftObject.getString("isrc"));
			newsInfo1.setMsg(newsInfoLeftObject.isNull("msg") ? ""
				: newsInfoLeftObject.getString("msg"));
			newsInfo1.setHeight(newsInfoLeftObject.getInt("iht"));
			duitangs.add(newsInfo1);
		    }
		}
	    } catch (JSONException e) {
		e.printStackTrace();
	    }
	    return duitangs;
	}
    }

    /**
     * 添加内容
     * 
     * @param pageindex
     * @param type
     *            1为下拉刷新 2为加载更多
     */
    private void AddItemToContainer(int pageindex, int type) {
	if (task.getStatus() != Status.RUNNING) {
	    String url = "http://www.duitang.com/album/1733789/masn/p/"
		    + pageindex + "/24/";
	    Log.d("MainActivity", "current url:" + url);
	    ContentTask task = new ContentTask(this, type);
	    task.execute(url);

	}
    }

    public class StaggeredAdapter extends BaseAdapter {
	private LinkedList<DuitangInfo> mInfos;

	public StaggeredAdapter(Context context) {
	    mInfos = new LinkedList<DuitangInfo>();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

	    ViewHolder holder;
	    DuitangInfo duitangInfo = mInfos.get(position);

	    if (convertView == null) {
		LayoutInflater layoutInflator = LayoutInflater.from(parent
			.getContext());
		convertView = layoutInflator.inflate(R.layout.infos_list, null);
		holder = new ViewHolder();
		holder.imageView = (ScaleImageView) convertView
			.findViewById(R.id.news_pic);
		holder.contentView = (TextView) convertView
			.findViewById(R.id.news_title);
		convertView.setTag(holder);
	    }

	    holder = (ViewHolder) convertView.getTag();
	    holder.imageView.setImageWidth(duitangInfo.getWidth());
	    holder.imageView.setImageHeight(duitangInfo.getHeight());
	    holder.contentView.setText(duitangInfo.getMsg());
	    System.out.println(duitangInfo.getIsrc());
	    mImageFetcher.loadImage(duitangInfo.getIsrc(), holder.imageView);
	    return convertView;
	}

	class ViewHolder {
	    ScaleImageView imageView;
	    TextView contentView;
	    TextView timeView;
	}

	@Override
	public int getCount() {
	    return mInfos.size();
	}

	@Override
	public Object getItem(int arg0) {
	    return mInfos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
	    return 0;
	}

	public void addItemLast(List<DuitangInfo> datas) {
	    mInfos.addAll(datas);
	}

	public void addItemTop(List<DuitangInfo> datas) {
	    for (DuitangInfo info : datas) {
		mInfos.addFirst(info);
	    }
	}
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.act_pull_to_refresh_sample);
	mAdapterView = (XListView) findViewById(R.id.list);
	mAdapterView.setPullLoadEnable(true);
	mAdapterView.setXListViewListener(this);

	mAdapter = new StaggeredAdapter(this);

	mImageFetcher = new ImageFetcher(this, 240);
	mImageFetcher.setLoadingImage(R.drawable.empty_photo);
	mImageFetcher.setImageCache(ImageCache.findOrCreateCache(this,
		ImageFetcher.HTTP_CACHE_DIR));
    }

    @Override
    protected void onResume() {
	super.onResume();
	mImageFetcher.setExitTasksEarly(false);
	mAdapterView.setAdapter(mAdapter);
	AddItemToContainer(currentPage, 2);
    }

    @Override
    public void onRefresh() {
	AddItemToContainer(++currentPage, 1);

    }

    @Override
    public void onLoadMore() {
	AddItemToContainer(++currentPage, 2);

    }
}// end of class
